package com.ahora.org.controller.biometrico.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DemoServlet
 */
@WebServlet("/ServletRequisitoPdf")
public class ServletRequisitoPdf extends HttpServlet {
	private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
		String nombreRequisito = request.getParameter("nombreRequisito");
		System.out.println("ME HAN LLAMADo");
		System.out.println("Nombre de archivo:@"+nombreRequisito+"@");
		File file = new File("C:/PAC/REQUISITOS/"+nombreRequisito);

		response.reset();
		response.setContentType("application/pdf");
		response.setContentLength((int) file.length());
		
		response.setHeader("Content-disposition", "inline; filename=\"" + file.getName() + "\"");
		BufferedInputStream input = null;
		BufferedOutputStream output = null;

		try {
		    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
		    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

		    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		    int length;
		    while ((length = input.read(buffer)) > 0) {
		        output.write(buffer, 0, length);
		    }
		} finally {
		    close(output);
		    close(input);
		}
	}
	
	private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
